package com.nyist.zk.controller;

import com.nyist.zk.entities.Patient;
import com.nyist.zk.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class PatientController {

    @Autowired
    private PatientService patientService;

    /************************ 病人对自己的操作开始***********************************/

    @PostMapping("/patients/login")
    public String patientLogin(HttpSession session, Patient patient, Map<String, Object> map) {
        if (!StringUtils.isEmpty(patient.getP_id())
                && !StringUtils.isEmpty(patient.getP_password())) {
            boolean flag = patientService.getPatientByIdAndPassword(patient.getP_id(), patient.getP_password());
            if (flag) {
                //登录成功
                Patient byIdPatient = patientService.findByIdPatient(patient.getP_id());
                session.setAttribute("loginUser", byIdPatient);
                System.out.println("登录成功!");
                return "patient/patientIndex";
            }
        }
        //登录失败
        System.out.println("输入的id : " + patient.getP_id());
        System.out.println("输入的password : " + patient.getP_password());
        map.put("msg", "用户名或密码错误");
        //  redirect 重定向
        return "patient/patientLogin";
    }

    @GetMapping("/patients/{p_id}")
    public String findPatient(@PathVariable("p_id") Integer p_id, Map<String, Object> map) {
        System.out.println("--------查看个人信息---------");
        Patient byIdPatientMap = patientService.findByIdPatient(p_id);
        map.put("byIdPatientMap", byIdPatientMap);
        return "patient/patientList";
    }

    @GetMapping("/patientUpdates/{p_id}")
    public String toUpdate(@PathVariable("p_id") Integer p_id, Map<String, Object> map) {
        System.out.println("--------准备修改个人信息---------");
        Patient byIdPatientMap = patientService.findByIdPatient(p_id);
        map.put("byIdPatientMap", byIdPatientMap);
        return "patient/patientUpdate";
    }

    @PutMapping("/patientUpdate")
    public String updatePatient(Patient patient, Map<String, Object> map) {
        System.out.println("--------------修改个人基本信息----------------");
        patientService.updatePatient(patient);
        Patient byIdPatientMap = patientService.findByIdPatient(patient.getP_id());
        map.put("byIdPatientMap", byIdPatientMap);
        return "patient/patientList";
    }

    @GetMapping("/patient/pwd")
    public String updatePatientPassword() {
        return "patient/patientPassword";
    }

    @ResponseBody
    @GetMapping("/patient/pwd/{oldPwd}")
    public Boolean checkPwd(HttpSession session, @PathVariable("oldPwd") String oldPwd) {
        System.out.println("原来的密码:" + oldPwd);
        //1.从Session中获取当前登录用户的User对象
        Patient patient = (Patient) session.getAttribute("loginUser");
        if (patient.getP_password().equals(oldPwd)) {
            //输入的旧密码正确
            return true;
        }
        return false;
    }

    @PostMapping("/patient/pwd")
    public String updatePwd(HttpSession session, String password) {
        //1.从Session中获取当前登录用户信息
        Patient patient = (Patient) session.getAttribute("loginUser");
        //2. 更新密码
        patient.setP_password(password);
        System.out.println("新的密码是:" + password);
        //3. 提交到数据库
        patientService.updatePatient(patient);
        //4. 注销重新登录
        return "redirect:/patient";
    }

    /************************ 病人对自己的操作结束***********************************/

    /************************ 管理者对病人的操作开始***********************************/

    @GetMapping("/doctorToPatients/{p_doctor}")
    public String doctorFindAllPatient(@PathVariable("p_doctor") Integer p_doctor,Map<String, Object> map) {
        System.out.println("-----------医生查看所有自己的病人开始-----------------");
        List<Patient> doctorFindAllPatient = patientService.doctorFindAllPatient(p_doctor);
        map.put("doctorFindAllPatient", doctorFindAllPatient);
        return "doctor/doctorFindAllPatientList";
    }

    @GetMapping("/doctorFindPatientByNameLike")
    public String doctorFindPatientByNameLike(Map<String, Object> map,Patient patient) {
        System.out.println("--------医生查看病人信息模糊查找---------");
        List<Patient> doctorFindAllPatient = patientService.doctorFindPatientByNameLike(patient.getP_doctor(),patient.getP_name());
        map.put("doctorFindAllPatient", doctorFindAllPatient);
        return "doctor/doctorFindAllPatientList";
    }

    @GetMapping("/doctorToPatientUpdate/{p_id}")
    public String doctorToPatientUpdate(@PathVariable("p_id") Integer p_id, Map<String, Object> map) {
        System.out.println("--------医生准备修改病人信息---------");
        Patient byIdPatientMap = patientService.findByIdPatient(p_id);
        map.put("byIdPatientMap", byIdPatientMap);
        return "doctor/doctorToPatientUpdate";
    }

    @PutMapping("/doctorToPatientUpdateAndPassword")
    public String doctorToPatientUpdateAndPassword(Patient patient, Map<String, Object> map) {
        System.out.println("--------------医生修改病人基本信息----------------");
        patientService.updatePatient(patient);
        List<Patient> doctorFindAllPatient = patientService.doctorFindAllPatient(patient.getP_doctor());
        map.put("doctorFindAllPatient", doctorFindAllPatient);
        return "doctor/doctorFindAllPatientList";
    }

    //前往添加 页面
    @GetMapping("/toDoctorAddPatientPage")
    public String toDoctorAddPatientPage() {
        System.out.println("----------------准备添加患者信息-------------------");
        return "doctor/doctorAddPatient";
    }

    //添加数据
    @PostMapping("/doctorAddPatientData")
    public String add(Patient patient, Map<String, Object> map) {
        //保存数据操作
        System.out.println("---------------添加患者----------------------------");
        patientService.insertPatient(patient);
        System.out.println("医生编号:"+patient.getP_doctor());
        List<Patient> doctorFindAllPatient = patientService.doctorFindAllPatient(patient.getP_doctor());
        map.put("doctorFindAllPatient", doctorFindAllPatient);
        return "doctor/doctorFindAllPatientList";
    }

    @DeleteMapping("/doctorDeletePatientData/{p_id}")
    public String deletePatientByPId(@PathVariable("p_id")Integer p_id, Map<String, Object> map){
        System.out.println("---------------删除患者----------------------------");
        Patient patient = patientService.findByIdPatient(p_id);
        patientService.deleteByIdPatient(p_id);
        List<Patient> doctorFindAllPatient = patientService.doctorFindAllPatient(patient.getP_doctor());
        map.put("doctorFindAllPatient", doctorFindAllPatient);
        return "doctor/doctorFindAllPatientList";
    }

    /************************ 管理者对病人的操作结束***********************************/


}
